package com.lk.api.own;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.lk.api.R;
import com.lk.api.own.StartService.MyBinder;

/**
 *
 * @author lk
 * 1.bindService startService���
 * startService���������activity��ͬ����
 * bindService���������activityͬ����
 * 2.������start or bind ֻҪserviceû��ʵ����ᱻcreate����startÿ�ζ������onStartCommand bind������Ѿ�ע�ᣬ�Ͳ������onbind
 *
 *
 *
 */
public class Start extends Activity {
	Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startservice);
		Log.d("Start_onCreate", "onCreate");
		Button aButton = (Button) findViewById(R.id.AstartService);
		Button bButton = (Button) findViewById(R.id.BstopService);
		Button iBinder = (Button) findViewById(R.id.iBindService);
		Button unBindService = (Button) findViewById(R.id.unBindService);
		Button mStartNewActivity = (Button) findViewById(R.id.StartNewActivity);
		Button tel = (Button) findViewById(R.id.tel);

		// ����һ��service
		aButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				intent = new Intent(Start.this, StartService.class);
				startService(intent);
			}
		});
		// ֹͣһ��service
		bButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				stopService(intent);

			}
		});
		// ����һ��bindService
		iBinder.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Start.this, StartService.class);
				bindService(intent, conn, Service.BIND_AUTO_CREATE);
			}
		});
		// ֹͣһ��unBindService
		unBindService.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				unbindService(conn);
			}
		});
		mStartNewActivity.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				intent = new Intent(Start.this, Start2.class);
				startActivity(intent);
			}
		});
		tel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				intent = new Intent();
				// ֱ�Ӳ���
				// intent.setAction(Intent.ACTION_CALL);
				// ����ú��� �ȴ���������
				intent.setAction(Intent.ACTION_DIAL);
				intent.setData(Uri.parse("tel:18609916531"));
				startActivity(intent);

			}
		});

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Log.d("aActivity", "onDestroy");
		super.onDestroy();
	}

	// ��������
	private ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			Log.d("onServiceDisconnected", "onServiceDisconnected");
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			// String names = ((MyBinder) service).helloWorld("����");
			// Toast.makeText(Start.this, names, Toast.LENGTH_LONG).show();
			Log.d("onServiceConnected", "onServiceConnected");
			((MyBinder) service).getService().helloService();

		}
	};
}
